//
//  LBMainViewController.swift
//  MicroBlog
//
//  Created by lj on 15/8/31.
//  Copyright © 2015年 itcast. All rights reserved.
//

import UIKit

class LSMainViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()

        addChildViewControllers()

    }
    
    override func viewWillAppear(animated: Bool) {
    
        super.viewWillAppear(animated)
        
        //在视图将要出现的时候才会创建Button
        setupComposedButton()

    }
    
    //添加撰写按钮，并设置其frame
    func setupComposedButton() {
    
        let count = childViewControllers.count
        
        let width = tabBar.bounds.width/CGFloat(count)
        
        composedButton.frame = CGRect(x: width * 2, y: 0, width: width, height: tabBar.bounds.height)
        
    }
    

    //懒加载创建Button
    private lazy var composedButton:UIButton = {
    
        let btn = UIButton()
        
        btn.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        btn.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        
        self.tabBar.addSubview(btn)
        
        //给按钮注册点击事件
        btn.addTarget(self, action: "cilckedComposedButton", forControlEvents: UIControlEvents.TouchUpInside)
        
        return btn
    }()
    
    //按钮的点击事件
    /**
    按钮监听方法，由运行循环来调用的，因此不能直接使用 private
    
    swift 中，所有的函数如果不使用 private 修饰，是全局共享的
    
    @objc 关键字能够保证运行循环能够调用，走的 oc 的消息机制，调用之前不再判断方法是否存在
    和 private 联用，就能够做到对方法的保护
    */
    @objc private func cilckedComposedButton() {
    
        let vc = LSUserAccountViewModel.sharedUserAccountViewModel.userLogon == true ? LSTweetStatusController() : LSOAuthViewController()
        let nav = UINavigationController(rootViewController: vc)
        
        presentViewController(nav, animated: true, completion: nil)
        
    }
    
    
    //添加子控制器
    private func addChildViewControllers() {
        
        addChildViewController(LSHomeTableViewController(), title: "首页", imageName: "tabbar_home")
        addChildViewController(LSMessageTableViewController(), title: "消息", imageName: "tabbar_message_center")
        addChildViewController(UIViewController())
        addChildViewController(LSDiscoverTableViewController(), title: "发现", imageName: "tabbar_discover")
        addChildViewController(LSProfileTableViewController(), title: "我", imageName: "tabbar_profile")
        
    }
  //添加单个子控制器
    private func addChildViewController(childController: UIViewController ,title:String , imageName: String) {
        
        childController.title = title
        childController.tabBarItem.image = UIImage(named: imageName)
        
        let nav = UINavigationController(rootViewController: childController)
        
        addChildViewController(nav)
        
    }

}
